package com.ruoyi.project.web.quesInfo.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.web.examPaper.domain.ExamPaper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.web.quesInfo.mapper.QuesInfoMapper;
import com.ruoyi.project.web.quesInfo.domain.QuesInfo;
import com.ruoyi.project.web.quesInfo.service.IQuesInfoService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 题目信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-22
 */
@Service
public class QuesInfoServiceImpl implements IQuesInfoService {

    private static final Logger log = LoggerFactory.getLogger(QuesInfoServiceImpl.class);
    @Autowired
    private QuesInfoMapper quesInfoMapper;

    /**
     * 查询题目信息
     *
     * @param quesId 题目信息ID
     * @return 题目信息
     */
    @Override
    public QuesInfo selectQuesInfoById(Long quesId) {
        return quesInfoMapper.selectQuesInfoById(quesId);
    }

    /**
     * 查询题目信息列表
     *
     * @param quesInfo 题目信息
     * @return 题目信息
     */
    @Override
    public List<QuesInfo> selectQuesInfoList(QuesInfo quesInfo) {
        return quesInfoMapper.selectQuesInfoList(quesInfo);
    }

    /**
     * 新增题目信息
     *
     * @param quesInfo 题目信息
     * @return 结果
     */
    @Override
    public int insertQuesInfo(QuesInfo quesInfo) {
        quesInfo.setCreateTime(DateUtils.getNowDate());
        return quesInfoMapper.insertQuesInfo(quesInfo);
    }

    /**
     * 修改题目信息
     *
     * @param quesInfo 题目信息
     * @return 结果
     */
    @Override
    public int updateQuesInfo(QuesInfo quesInfo) {
        return quesInfoMapper.updateQuesInfo(quesInfo);
    }

    /**
     * 删除题目信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteQuesInfoByIds(String ids) {
        return quesInfoMapper.deleteQuesInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除题目信息信息
     *
     * @param quesId 题目信息ID
     * @return 结果
     */
    @Override
    public int deleteQuesInfoById(Long quesId) {
        return quesInfoMapper.deleteQuesInfoById(quesId);
    }

    @Override
    public String importQues(List<QuesInfo> quesList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(quesList) || quesList.size() == 0) {
            throw new BusinessException("导入题目数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String operName = ShiroUtils.getLoginName();
        for (QuesInfo ques : quesList) {
            if (StringUtils.isEmpty(ques.getLearnType())) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、题目 " + ques.getQuesDesc() + " 未指定题目类型");
                continue;
            }
            try {
                ques.setAnsStandard(ques.getAnsStandard().toUpperCase());
                // 验证是否存在
                QuesInfo info = quesInfoMapper.selectItemByDesc(ques.getQuesDesc());
                if (StringUtils.isNull(info)) {
                    ques.setCreateBy(operName);
                    this.insertQuesInfo(ques);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、题目 " + ques.getQuesDesc() + " 导入成功");
                } else if (isUpdateSupport) {
                    ques.setUpdateBy(operName);
                    ques.setQuesId(info.getQuesId());
                    this.updateQuesInfo(ques);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、题目 " + ques.getQuesDesc() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、题目 " + ques.getQuesDesc() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、题目 " + ques.getQuesDesc() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public Map<String,Object> toRandomList(String learnType, Integer quesNum,String chooseType) {
        return quesInfoMapper.toRandomList(learnType,quesNum,chooseType);
    }

    @Override
    public List<QuesInfo> queryListByIds(String quesIds) {
        return quesInfoMapper.queryListByIds(Convert.toStrArray(quesIds));
    }
}
